package com.fjwt.gz.mgr.ctrl.stat;

import com.alibaba.fastjson2.JSONObject;
import com.fjwt.gz.components.mq.model.ExportExcelMQ;
import com.fjwt.gz.components.mq.vender.IMQSender;
import com.fjwt.gz.core.constants.Constants;
import com.fjwt.gz.core.model.ApiRes;
import com.fjwt.gz.db.dto.StatMemberNumDTO;
import com.fjwt.gz.mgr.ctrl.CommonCtrl;
import com.fjwt.gz.service.impl.StatMemberNumService;
import com.fjwt.gz.service.impl.SysExportTaskService;
import com.fjwt.gz.service.other.export.StatMemberNumExportReport;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 统计：机构会员通报统计
 * </p>
 *
 * @author shiyonghao
 * @since 2025-08-21
 */
@Slf4j
@RestController
@RequestMapping("/api/statMemberNum")
public class StatMemberNumController extends CommonCtrl {

    @Autowired
    private StatMemberNumService statMemberNumService;
    @Autowired
    private IMQSender mqSender;
    @Autowired
    private SysExportTaskService sysExportTaskService;

    /**
     * 会员通报（机构明细）分页列表
     */
    @GetMapping("/agent/details")
    public ApiRes agentDetails() {
        StatMemberNumDTO statMemberNumDTO = getObject(StatMemberNumDTO.class);
        return ApiRes.page(statMemberNumService.getAgentDetails(getIPage(), statMemberNumDTO));
    }

    /**
     * 郑邮享会员发展情况通报列表
     */
    @GetMapping("/development/situation")
    public ApiRes developmentSituation() {
        StatMemberNumDTO statMemberNumDTO = getObject(StatMemberNumDTO.class);
        return ApiRes.ok(statMemberNumService.getDevelopmentSituation(statMemberNumDTO));
    }

    /**
     * 会员通报（机构明细）导出
     */
    @GetMapping("/agent/details/export")
    public ApiRes detailsExport() {
        StatMemberNumDTO statMemberNumDTO = getObject(StatMemberNumDTO.class);
        statMemberNumDTO.setExportType(StatMemberNumExportReport.AGENT_DETAILS);
        // 报表名称
        String fileName = "会员通报表(机构明细)";
        // 添加导出任务记录
        Long exportTaskId = sysExportTaskService.addExportTask(getCurrentUser().getSysUser(), fileName, "StatMemberNum",
                Constants.EXPORT_TASK_ENUM.RESOURCE_TYPE.ACTIVITY, JSONObject.toJSONString(statMemberNumDTO));
        // 发送mq
        mqSender.send(ExportExcelMQ.build(exportTaskId));
        return ApiRes.ok();
    }

    /**
     * 郑邮享会员发展情况通报列表导出
     */
    @GetMapping("/development/situation/export")
    public ApiRes agentRecallDetailsExport() {
        StatMemberNumDTO statMemberNumDTO = getObject(StatMemberNumDTO.class);
        statMemberNumDTO.setExportType(StatMemberNumExportReport.DEVELOPMENT_SITUATION);
        // 报表名称
        String fileName = "“郑邮享”会员发展情况通报";
        // 添加导出任务记录
        Long exportTaskId = sysExportTaskService.addExportTask(getCurrentUser().getSysUser(), fileName, "StatMemberNum",
                Constants.EXPORT_TASK_ENUM.RESOURCE_TYPE.ACTIVITY, JSONObject.toJSONString(statMemberNumDTO));
        // 发送mq
        mqSender.send(ExportExcelMQ.build(exportTaskId));
        return ApiRes.ok();
    }

}

